﻿using UnityEngine;

namespace Core.Setting
{
    public partial class ScreenSetting
    {
        /// <summary>
        /// IOS, Android
        /// </summary>
        private class ScreenNative : AbstractScreenPlatform
        {

            public override void Awake(ScreenSetting owner)
            {
                base.Awake(owner);

                Scrn.SafeArea = Screen.safeArea;

                Scrn.Size = new WH(Screen.width, Screen.height);
                var resolution = Screen.currentResolution;
                Scrn.Resolution = new WH(resolution.width, resolution.height);

                log.err(Scrn.SafeArea);
                log.err($"Screen.size={Scrn.Size} {Screen.currentResolution}");
                log.err($"Screen.orientation ={Screen.orientation}");

                //if (IsPortrait() == false)
                //{
                //    Orientation = ScreenOrientation.Portrait;

                //    Screen.orientation = ScreenOrientation.Portrait;

                //    Scrn.Listen();
                //}
            }

            public override void Set(ESceneOrientation ori)
            {
                if (ori == ESceneOrientation.Portrait)
                {
                    if (IsLandscape())
                    {
                        Set(ScreenOrientation.Portrait);
                        SetRotate(false);

                        Scrn.Listen();
                    }
                }
                else
                {
                    if (IsPortrait())
                    {
                        Set(ScreenOrientation.LandscapeLeft);
                        SetRotate(true);

                        Scrn.Listen();
                    }
                }
            }

            private void Set(ScreenOrientation ori)
            {
                Screen.orientation = ori;
                Orientation = ori;
            }


            private void SetRotate(bool b)
            {
                Screen.autorotateToLandscapeLeft = b;
                Screen.autorotateToLandscapeRight = b;
                Screen.autorotateToPortrait = !b;
                Screen.autorotateToPortraitUpsideDown = !b;
            }

        }
    }
}
